﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//对象池管理类
public class GameObjectPoolMgr {
    private static Dictionary<object, GameObjectPool> poolMap = new Dictionary<object, GameObjectPool>();
	
	/// <summary>
	/// 初始化对象池
	/// </summary>
	/// <param name="poolName"></param>
	/// <param name="prefab"></param>
	public static void InitPool(string poolName,GameObject prefab)
	{
        if (poolMap.ContainsKey(poolName)) throw NotImplementedException("对象池已存在...");
        poolMap.Add(poolName,new GameObjectPool(prefab));
	}
	/// <summary>
	/// 获取对象
	/// </summary>
	/// <param name="poolName"></param>
	/// <param name="position"></param>
	/// <returns></returns>
    public static GameObject GetObject(string poolName, Vector3 position = default(Vector3), Quaternion quaternion = default(Quaternion))
	{
        if (!poolMap.ContainsKey(poolName)) throw NotImplementedException("不存在的对象池...");
        var pool = poolMap.GetValue(poolName);
        return pool.GetObject(position, quaternion);
	}
    /// <summary>
    /// 释放对象池 默认移除所有
    /// 如需移除指定对象池传名字
    /// </summary>
    /// <param name="poolName"></param>
    public static void ReleasePool(string poolName = "All")
    {
        if (poolName == "All")
        {
            foreach (var item in poolMap)
            {
                item.Value.ReleasePool();
            }
            poolMap.Clear();
        }
        else
        {
            if (!poolMap.ContainsKey(poolName)) throw NotImplementedException("不存在的对象池...");
            var pool = poolMap.GetValue(poolName);
            pool.ReleasePool();
            poolMap.Remove(poolName);
        }
    }
    /// <summary>
    /// 放回对象池
    /// </summary>
    /// <param name="poolName"></param>
    /// <param name="_object"></param>
    public static void PutObject(string poolName, GameObject _object)
    {
        if (!poolMap.ContainsKey(poolName)) throw NotImplementedException("不存在的对象池...");
        var pool = poolMap.GetValue(poolName);
        pool.PutObject(_object);
        poolMap.AddOrReplace(poolName,pool);
    }
    /// <summary>
    /// 对象总个数
    /// </summary>
    /// <param name="poolName"></param>
    /// <param name="_object"></param>
    public static void Count(string poolName)
    {
        if (!poolMap.ContainsKey(poolName)) throw NotImplementedException("不存在的对象池...");
        var pool = poolMap.GetValue(poolName);
        Debug.Log(poolName + " Count:" + pool.Count());
    }
    /// <summary>
    /// 可用对象个数
    /// </summary>
    /// <param name="poolName"></param>
    /// <param name="_object"></param>
    public static void Useable(string poolName)
    {
        if (!poolMap.ContainsKey(poolName)) throw NotImplementedException("不存在的对象池...");
        var pool = poolMap.GetValue(poolName);
        Debug.Log(poolName + " Count:" + pool.Useable());
    }
    /// <summary>
    /// 抛错
    /// </summary>
    /// <param name="p"></param>
    /// <returns></returns>
    private static System.Exception NotImplementedException(string p)
    {
        throw new System.NotImplementedException(p);
    }
}
